﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class CreateBooking : Form
    {
        ArrayList customerDetails = new ArrayList();
        string recordLblCheckOut;
        string tempCustomerID;
        public CreateBooking()
        {
            InitializeComponent();
            timer1.Start();
            lblcholdername.Visible = false;
            lblcnumber.Visible = false;
            cboxcnumber.Visible = false;
            btnaddccardinfo.Visible = false;
            recordLblCheckOut = lblShowCheckOut.Text;
            customerDetails = ManageCustomerDBManager.GetAllCustomerDetails();
            cboxnric.Items.Add("-Select NRIC-");
            cboxnric.SelectedIndex = 0;
            if(customerDetails.Count == 0)
            {
                MessageBox.Show("No Customer Details");
                this.Hide();
                return;
            }

            foreach(Customer c in customerDetails)
            {
                cboxnric.Items.Add(c.Nric.ToString());
            }
        }

        private void btncheckroom_Click(object sender, EventArgs e)
        {
            if(rbno.Checked != true && rbyes.Checked != true)
            {
                MessageBox.Show("Kindly indicate whether you want smoking or non-smoking");
            }

            else if (rbyes.Checked == true)
            {
                //search base on smoking
                if (lblShowCheckOut.Text != recordLblCheckOut)
                {
                    RoomBookingAvaliability newForm = new RoomBookingAvaliability("Yes", dtpcalender.Value.Date, Convert.ToDateTime(lblShowCheckOut.Text), tbxAddDays.Text, this, null);
                    newForm.ShowDialog();
                }

                else
                {
                    MessageBox.Show("please select check in date and add number of days staying and press add");
                }
            }

            else
            {
                //search base on non smoking
                if (lblShowCheckOut.Text != recordLblCheckOut)
                {
                    RoomBookingAvaliability newForm = new RoomBookingAvaliability("No", dtpcalender.Value.Date, Convert.ToDateTime(lblShowCheckOut.Text), tbxAddDays.Text, this, null);
                    newForm.ShowDialog();
                }

                else
                {
                    MessageBox.Show("please select check in date and add number of days staying and press add");
                }
            }
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            lbltime.Text = dt.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int days = int.Parse(tbxAddDays.Text);
            days = days - 1;
            string day = dtpcalender.Value.AddDays(days).ToLongDateString();
            lblShowCheckOut.Text = day;
        }

        private void btnDeposit_Click(object sender, EventArgs e)
        {   
            if(lblshowamount.Text.Equals("-"))
            {
                MessageBox.Show("you have not selected any rooms yet, please check click on check room 1st");
                return;
            }
            double deposit = Convert.ToDouble(tbxDeposit.Text);
            double balance = Convert.ToDouble(lblshowamount.Text) - deposit;
            lblshowbalance.Text = Convert.ToString(balance);
        }

        private void btnBook_Click(object sender, EventArgs e)
        {
            string newCheckInId = "CKID" + DateTime.Now.GetHashCode();
            
            while (AdvBookingDBManager.testWhetherCheckIDExists(newCheckInId) == true)
            {
                newCheckInId = "CKID" + DateTime.Now.GetHashCode();
            }

            if (rbccard.Checked == true && cboxcnumber.SelectedIndex == 0)
            {
                MessageBox.Show("Please check all information are filled in");
                return;
            }

            if (cboxnric.SelectedIndex == 0 || tbxadult.Text.Equals("") || tbxchilden.Text.Equals("") || tbxAddDays.Text.Equals("")
               || lblShowCheckOut.Text.Equals("checkoutdate") || lblshowbalance.Text.Equals("-"))
            {
                MessageBox.Show("Please check all information are filled in");
                return;
            }
            else
            {
                CheckInOut bookingInfo = new CheckInOut();
                bookingInfo.CheckID = newCheckInId;
                bookingInfo.Deposit = Convert.ToDecimal(tbxDeposit.Text);
                bookingInfo.Balance = Convert.ToDecimal(lblshowbalance.Text);
                bookingInfo.CheckInTime = dtpcalender.Value.Date;
                bookingInfo.CheckOutTime = Convert.ToDateTime(lblShowCheckOut.Text);
                bookingInfo.NumOfAdult = Convert.ToInt32(tbxadult.Text);
                bookingInfo.NumOfChild = Convert.ToInt32(tbxchilden.Text);
                bookingInfo.NumOfKey = 0;
                bookingInfo.ExtraCostTotal = 0;
                bookingInfo.RoomRate = Convert.ToDecimal(lblshowrmprice.Text);
                bookingInfo.Remarks = tbxremarks.Text;
                bookingInfo.RoomNo = lblshowrmno.Text;
                bookingInfo.CustomerID = tempCustomerID;
                bookingInfo.NumOfDays = Convert.ToInt32(tbxAddDays.Text);
                bookingInfo.RoomStatus = "Booked";

                if (rbcash.Checked)
                {
                    bookingInfo.PaymentType = rbcash.Text.ToString();
                }

                else
                {
                    bookingInfo.PaymentType = rbccard.Text.ToString();
                }

                if (AdvBookingDBManager.InsertNewBookingInfo(bookingInfo) == 1)
                {
                    MessageBox.Show("Sucessfully Created New Booking, ID: " + bookingInfo.CheckID);
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Create was not sucessful");
                }
            }


        }

        private void rbcash_CheckedChanged(object sender, EventArgs e)
        {
            lblcholdername.Visible = false;
            lblcnumber.Visible = false;
            lblshowcholdername.Visible = false;
            cboxcnumber.Visible = false;
            btnaddccardinfo.Visible = false;
        }

        private void rbccard_CheckedChanged(object sender, EventArgs e)
        {
            cboxcnumber.Items.Clear();
            ArrayList tempCreditCardInfo = new ArrayList();
            if(rbccard.Checked == true)
            {
                foreach (Customer c in customerDetails)
                {
                    if (c.Nric.Equals(cboxnric.SelectedItem.ToString()))
                    {
                        if (ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID).Count == 0)
                        {
                            if ((MessageBox.Show("Do you want to create new credit card info?", "Confirm Create?", MessageBoxButtons.YesNo) == DialogResult.Yes))
                            {
                                this.Hide();
                                CreateCreditCard newMDIChild = new CreateCreditCard();
                                callForm(newMDIChild);
                                return;
                            }
                            rbcash.Checked = true;
                            return;
                        }
                        if (ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID).Count > 0)
                        {
                            cboxcnumber.Items.Clear();
                            tempCreditCardInfo = ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID);
                            lblcholdername.Visible = true;
                            lblcnumber.Visible = true;
                            lblshowcholdername.Visible = true;
                            cboxcnumber.Visible = true;
                            btnaddccardinfo.Visible = true;
                            cboxcnumber.Items.Add("-Select Card-");
                            foreach (CreditCardInfo cCardInfo in tempCreditCardInfo)
                            {
                                cboxcnumber.Items.Add(cCardInfo.CreditCardNo);
                            }
                            cboxcnumber.SelectedIndex = 0;
                            lblshowcholdername.Text = c.FirstName + " " + c.LastName;
                            return;
                        }
                    }
                }       
            }
            
        }

        private void cboxnric_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach(Customer c in customerDetails)
            {
                lblshowfirstname.Text = "-";
                lblshowlastname.Text = "-";
                lblshownric.Text = "-";
                tbxshowaddress.Text = "";
                lblshowpostalcode.Text = "-";
                lblshowcountry.Text = "-";
                lblshowdob.Text = "-";
                lblshowcontactnumber.Text = "-";
                tbxshowemail.Text = "";
                        
                if(cboxnric.SelectedItem.ToString().Equals(c.Nric))
                {   
                    //customer
                    lblshowfirstname.Text = c.FirstName;
                    lblshowlastname.Text = c.LastName;
                    lblshownric.Text = c.Nric;
                    tbxshowaddress.Text = c.HomeAddress;
                    lblshowpostalcode.Text = Convert.ToString(c.PostalCode);
                    lblshowcountry.Text = c.Country;
                    lblshowdob.Text = Convert.ToString(c.Dob);
                    lblshowcontactnumber.Text = Convert.ToString(c.ContactNo);
                    tbxshowemail.Text = c.Email;   
                    //credit card
                    if (ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID).Count == 0)
                    {
                        lblcholdername.Visible = false;
                        lblcnumber.Visible = false;
                        lblshowcholdername.Visible = false;
                        cboxcnumber.Visible = false;
                        btnaddccardinfo.Visible = false;
                        MessageBox.Show("No Credit Card Info");
                        rbcash.Checked = true;
                        tempCustomerID = c.CustomerID;
                        return;
                    }
                    if (ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID).Count > 0)
                    {
                        cboxcnumber.Items.Clear();
                        ArrayList tempCreditCardInfo = new ArrayList();
                        tempCreditCardInfo = ManageCustomerDBManager.GetCreditInfoByCustomerId(c.CustomerID);
                        cboxcnumber.Items.Add("-Select Card-");
                        foreach (CreditCardInfo cCardInfo in tempCreditCardInfo)
                        {
                            cboxcnumber.Items.Add(cCardInfo.CreditCardNo);
                        }
                        cboxcnumber.SelectedIndex = 0;
                        lblshowcholdername.Text = c.FirstName + " " + c.LastName;
                        tempCustomerID = c.CustomerID;
                        return;
                    }
                    
                    return;
                }
            }


        }

        private void btnnewcustomer_Click(object sender, EventArgs e)
        {
            this.Hide();
            CreateCustomer newMDIChild = new CreateCustomer();
            callForm(newMDIChild);
        }

        private void tbxCheckAllForNumbers_TextChanged(object sender, EventArgs e) // int only generalized method
        {
            Exception X = new Exception();
            TextBox T = (TextBox)sender;

            try
            {
                if (T.Text != "-")
                {
                    int x = int.Parse(T.Text);
                }
            }
            catch (Exception)
            {
                try
                {
                    int CursorIndex = T.SelectionStart - 1;
                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index
                    T.SelectionStart = CursorIndex;
                    T.SelectionLength = 0;
                }
                catch (Exception) { }
            }
        }

        private void checkWhetherIsItDoubletextbox_TextChanged(object sender, EventArgs e) // double only generalized method
        {
            Exception X = new Exception();

            TextBox T = (TextBox)sender;

            try
            {
                if (T.Text != "-")
                {
                    double x = double.Parse(T.Text);

                    if (T.Text.Contains(','))
                        throw X;
                }
            }
            catch (Exception)
            {
                try
                {
                    int CursorIndex = T.SelectionStart - 1;
                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index
                    T.SelectionStart = CursorIndex;
                    T.SelectionLength = 0;
                }
                catch (Exception) { }
            }
        }

        Form mdiChildForm = new Form();
        public void Form1_Load(object sender, System.EventArgs e)
        {
            MdiClient ctlMDI;

            foreach (Control ctl in MainForm.ActiveForm.Controls)
            {
                try
                {
                    // Attempt to cast the control to type MdiClient.
                    ctlMDI = (MdiClient)ctl;

                    // Set the BackColor of the MdiClient control.
                    ctlMDI.BackColor = this.BackColor;
                }
                catch (InvalidCastException exc)
                {
                    // Catch and ignore the error if casting failed.
                }
            }

            // Set the IsMdiContainer property to true.
            IsMdiContainer = true;

            // Set the child form's MdiParent property to  
            // the current form.
            mdiChildForm.MdiParent = MainForm.ActiveForm;

        }

        public void callForm(Form newForm)
        {
            if (CheckOpened(newForm.Text) == false)
            {
                newForm.MdiParent = MainForm.ActiveForm;
                // Display the new form.
                newForm.StartPosition = FormStartPosition.CenterScreen;
                newForm.Show();
            }
        }

        public bool CheckOpened(string name)
        {
            FormCollection fc = Application.OpenForms;
            foreach (Form frm in fc)
            {
                if (frm.Text == name)
                {
                    return true;
                }
            }
            return false;
        }

        private void btnaddccardinfo_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Do you want to create new credit card info?", "Confirm Create?", MessageBoxButtons.YesNo) == DialogResult.Yes))
            {
                this.Hide();
                CreateCreditCard newMDIChild = new CreateCreditCard();
                callForm(newMDIChild);
            }
        }



    }
}
